package com.heima.controller.user_service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.heima.pojo.dto.LoginDto;
import com.heima.pojo.dto.UserDto;
import com.heima.pojo.entity.Node;
import com.heima.pojo.entity.User;
import com.heima.pojo.vo.LoginUserVo;
import com.heima.pojo.vo.PageVo;
import com.heima.pojo.vo.UserPageVo;
import com.heima.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/api/user-service/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 用户登录
     * @param dto
     * @return
     */
    @PostMapping("/login")
    public LoginUserVo login(@RequestBody LoginDto dto){
       return userService.login(dto);
    }

    /**
     * 人员搜索 条件分页查询
     * @param pageIndex
     * @param pageSize
     * @param userName
     * @return
     */
    @GetMapping("/search")
    public PageVo<UserPageVo> userPage(@RequestParam Integer pageIndex, Integer pageSize,
                                       @RequestParam(required = false) String userName){
        PageVo<UserPageVo> userPage = userService.getUserList(pageIndex,pageSize,userName);
        return userPage;
    }

    /**
     * 新增人员
     * @param userDto
     * @return
     */
    @PostMapping
    public Boolean save(@RequestBody UserDto userDto){
        User user = userDto.toUser();
       return userService.save(user);
    }

    /**
     * 修改用户信息
     * @param userId
     * @param userDto
     * @return
     */
    @PutMapping("/{userId}")
    public Boolean update(@PathVariable Long userId, @RequestBody UserDto userDto){
        User user = userDto.toUser();
        Boolean result = userService.update(userId,user);
        return result;
    }

    @DeleteMapping("/{userId}")
    public Boolean delete(@PathVariable String userId){
        log.info("根据用户id删除用户 {}", userId);
        Long id = Long.valueOf(userId);
       Boolean result = userService.delete(id);
       return result;
    }



}
